<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;


use app\common\lib\Show;
use app\common\validate\Wechat as ValObj;
use app\common\model\mysql\Wechat as ModelObj;
use think\Exception;
use think\facade\Log;

class Wechat extends Base
{
    /**
     * @var ValObj
     */
    protected ValObj $valObj;
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    /**
     * @param array $data
     * @return ModelObj|array|false|\think\Model|null
     * @throws Exception
     */
    public function WechatByOpenid(array $data = [])
    {
        try {
            $userinfo = $this->modelObj->getWechatByOpenid($data['openid']);
        } catch (\Exception $e) {
            throw new Exception("系统内部错误");
        }
        $data['ret_data'] = Show::json_encode($data);
        if ($userinfo) {
            $result = $userinfo->save($data);
        } else {
            $result = $this->modelObj->insertAdd($data);
        }
        if ($result) {
            if ($userinfo) {
                return $userinfo->toArray();
            } else {
                return $data;
            }
        } else {
            return false;
        }
    }

    public function getOpenidById(string $openid = '')
    {
        try {
            $res = $this->modelObj->getOpenidById($openid);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $res;
    }

    /**
     * @param array $where
     * @return int|mixed
     * @throws Exception
     */
    public function getIndexWechatByCount(array $where = [])
    {
        try {
            $count = $this->modelObj->getIndexWechatByCount($where);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if (!$count) {
            return 0;
        }
        return $count['count'];
    }

    /**
     * @param string $nickName
     * @return array
     * @throws Exception
     */
    public function getWechatByNickName(string $nickName = '')
    {
        try {
            $result = $this->modelObj->getWechatByNickName($nickName);
        } catch (\Exception $e) {
            throw new Exception("查询错误");
        }
        return $result;
    }

    public function addWechat(array $data = [])
    {
        try {
            $res = $this->getOpenidById((string)$data['openid']);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        try {
            if ($res) {
                $result = $res->save($data);
            } else {
                $result = $this->modelObj->insertAdd($data);
            }
        } catch (\Exception $e) {
            Log::error($e->getMessage() . " " . $e->getFile() . " " . $e->getLine());
            throw new Exception("操作失败");
        }
        return $result;
    }

    /**
     * @param string $name
     * @return array
     * @throws Exception
     */
    public function getWechatByRealNameByIds(string $name = '')
    {
        try {
            $wechat = $this->modelObj->getWechatByRealNameByIds($name);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $wechat;
    }

}